package com.klhappy.houyuan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.klhappy.houyuan.R;
import com.klhappy.houyuan.fragement.DingDan_Fragment;
import com.klhappy.houyuan.fragement.Houyuan_Fragment;
import com.klhappy.houyuan.fragement.Jiayuan_Fragment;
import com.klhappy.houyuan.fragement.Lanzi_Fragment;
import com.klhappy.houyuan.utils.NotifyMethod;
import com.klhappy.houyuan.view.BaseFragmentActivity;

//首页界面
public class Houyuan_MainActivity extends BaseFragmentActivity implements OnClickListener {

    private Houyuan_Fragment fg1;
    private Lanzi_Fragment fg2;
    private DingDan_Fragment fg3;
    private Jiayuan_Fragment fg4;
    private RelativeLayout houyuan_layout;
    private RelativeLayout lanzi_layout;
    private RelativeLayout fengshou_layout;
    private RelativeLayout jiayuan_layout;
    private ImageView houyuan_image;
    private ImageView lanzi_image;
    private ImageView fengshou_image;
    private ImageView jiayuan_image;
    private TextView houyuan_text;
    private TextView fengshou_text;
    private TextView lanzi_text;
    private TextView jiayuan_text;
    private int whirt = 0xFFFFFFFF;
    private int gray = 0xFF6C6E70;
    private int green = 0xFF639800;
    private FragmentManager fManager;

    @InjectView(R.id.tv_checkin)
    TextView tvCheckin;
    @InjectView(R.id.ib_search)
    ImageButton ibSearch;
    @InjectView(R.id.linear_no)
    RelativeLayout linear_no;
    @InjectView(R.id.title_img)
    ImageView title_img;
    @InjectView(R.id.title_text)
    TextView title_text;
    @InjectView(R.id.click_search)
    LinearLayout click_search;

    @OnClick(R.id.click_checkin)
    public void onClickCheckin() {
        startActivity(new Intent(this, LoginActivity.class));
    }

    @OnClick(R.id.click_search)
    public void onClickSearch() {
        startActivity(new Intent(this, SearchProductActivity.class));
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.houyuan_main);
        ButterKnife.inject(this);

        fManager = getSupportFragmentManager();
        initViews();
        setChioceItem(0);
    }

    public void initViews() {
        houyuan_image = (ImageView) findViewById(R.id.houyuan_image);
        lanzi_image = (ImageView) findViewById(R.id.lanzi_image);
        fengshou_image = (ImageView) findViewById(R.id.fengshou_image);
        jiayuan_image = (ImageView) findViewById(R.id.jiayuan_image);
        houyuan_text = (TextView) findViewById(R.id.houyuan_text);
        lanzi_text = (TextView) findViewById(R.id.lanzi_text);
        fengshou_text = (TextView) findViewById(R.id.fengshou_text);
        jiayuan_text = (TextView) findViewById(R.id.jiayuan_text);
        houyuan_layout = (RelativeLayout) findViewById(R.id.houyuan_layout);
        lanzi_layout = (RelativeLayout) findViewById(R.id.lanzi_layout);
        fengshou_layout = (RelativeLayout) findViewById(R.id.fengshou_layout);
        jiayuan_layout = (RelativeLayout) findViewById(R.id.jiayuan_layout);

        houyuan_layout.setOnClickListener(this);
        lanzi_layout.setOnClickListener(this);
        fengshou_layout.setOnClickListener(this);
        jiayuan_layout.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.houyuan_layout:
                setChioceItem(0);
                break;
            case R.id.lanzi_layout:
                setChioceItem(1);
                break;
            case R.id.fengshou_layout:
                setChioceItem(2);
                break;
            case R.id.jiayuan_layout:
                setChioceItem(3);
                break;
            default:
                break;
        }
    }

    public void setChioceItem(int index) {
        FragmentTransaction transaction = fManager.beginTransaction();
        clearChioce();
        hideFragments(transaction);
        switch (index) {
            case 0:
                title_img.setVisibility(View.VISIBLE);
                title_text.setVisibility(View.GONE);
                click_search.setVisibility(View.VISIBLE);
                linear_no.setVisibility(View.VISIBLE);
                houyuan_image.setImageResource(R.drawable.ic_tabbar_houyuan_pressed);
                houyuan_text.setTextColor(green);
                if (fg1 == null) {
                    fg1 = new Houyuan_Fragment();
                    transaction.add(R.id.content, fg1);
                } else {
                    transaction.show(fg1);
                }
                break;

            case 1:
                title_img.setVisibility(View.GONE);
                title_text.setVisibility(View.VISIBLE);
                title_text.setText("篮子");
                click_search.setVisibility(View.GONE);
                linear_no.setVisibility(View.VISIBLE);
                lanzi_image.setImageResource(R.drawable.ic_tabbar_lanzi_pressed);
                lanzi_text.setTextColor(green);
                if (fg2 == null) {
                    fg2 = new Lanzi_Fragment();
                    transaction.add(R.id.content, fg2);
                } else {
                    transaction.show(fg2);
                }
                NotifyMethod.getInstance().postNotification(Lanzi_Fragment.MV);
                break;

            case 2:
                title_img.setVisibility(View.GONE);
                title_text.setVisibility(View.VISIBLE);
                title_text.setText("订单");
                click_search.setVisibility(View.GONE);
                linear_no.setVisibility(View.VISIBLE);
                fengshou_image.setImageResource(R.drawable.ic_tabbar_fengshou_pressed);
                fengshou_text.setTextColor(green);
                if (fg3 == null) {
                    fg3 = new DingDan_Fragment();
                    transaction.add(R.id.content, fg3);
                } else {
                    transaction.show(fg3);
                }
                break;

            case 3:
                title_img.setVisibility(View.GONE);
                title_text.setVisibility(View.VISIBLE);
                title_text.setText("家园");
                click_search.setVisibility(View.GONE);
                linear_no.setVisibility(View.GONE);
                jiayuan_image.setImageResource(R.drawable.ic_tabbar_jiayuan_pressed);
                jiayuan_text.setTextColor(green);
                if (fg4 == null) {
                    fg4 = new Jiayuan_Fragment();
                    transaction.add(R.id.content, fg4);
                } else {
                    transaction.show(fg4);
                }
                break;
        }
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (fg1 != null) {
            transaction.hide(fg1);
        }
        if (fg2 != null) {
            transaction.hide(fg2);
        }
        if (fg3 != null) {
            transaction.hide(fg3);
        }
        if (fg4 != null) {
            transaction.hide(fg4);
        }
    }

    public void clearChioce() {
        houyuan_image.setImageResource(R.drawable.ic_tabbar_houyuan_normal);
        houyuan_layout.setBackgroundColor(whirt);
        houyuan_text.setTextColor(gray);
        lanzi_image.setImageResource(R.drawable.ic_tabbar_lanzi_normal);
        lanzi_layout.setBackgroundColor(whirt);
        lanzi_text.setTextColor(gray);
        fengshou_image.setImageResource(R.drawable.ic_tabbar_fengshou_normal);
        fengshou_layout.setBackgroundColor(whirt);
        fengshou_text.setTextColor(gray);
        jiayuan_image.setImageResource(R.drawable.ic_tabbar_jiayuan_normal);
        jiayuan_layout.setBackgroundColor(whirt);
        jiayuan_text.setTextColor(gray);
    }
}
